package com.eascs.banknet.boc.dao;

import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.eascs.app.dao.JdbcSession;
import com.eascs.banknet.boc.vo.bocs02.BOCS02Bean;
import com.eascs.banknet.boc.vo.bocs02.BOCS02Element;
import com.eascs.banknet.common.dao.BaseO2oDao;
import com.eascs.banknet.constant.FileTypeConstants;
import com.eascs.banknet.data.dao.DditemDao;
import com.eascs.common.util.StringUtil;

@Repository
public class BOCS02Dao extends BaseO2oDao{
	private Map<String, Object> cityMap = null;
	private Map<String, Object> provinceMap = null;
	private Map<String, Object> countyMap = null;
	@Inject
	private DditemDao dditemDao;
	@Inject
	private JdbcSession workflowJdbcSession;
	@Inject
	protected JdbcSession crmJdbcSession;
	public BOCS02Bean queryData(String startId){
		StringBuffer sql = new StringBuffer();
		sql.append(" select t2.loanapply_no 'loanapplyNo', t2.loan_id 'customerId',")
			.append(" t2.bank_applyno 'APPNO',")
			.append(" t1.bank_account 'RECEIVEACCNO',t1.bank_account 'LAFPACT',")//还款账号
			.append(" t1.receive_bank 'RECEIVEBNK', ")//贷款发放开户行
			.append(" t1.loan_term 'LAFTERM', ")//贷款期限
			.append(" ROUND(t1.ea_mount,2) 'LAFDRAMT', ")//贷款金额
			//.append(" t1.agency ")
			.append(" t1.ltd 'unitNo' ")//经营单位
			.append(" from o2o_loanapply t1, o2o_loan_user t2 ")
			.append(" where t1.loanapply_no = t2.loanapply_no and (t1.data_source is null or t1.data_source!='2') ")
			.append(" and t2.spouse_id is null")//去掉配偶
			.append(" and t1.wf_type in ('NF','S')")
			.append(" and  t2.bank_status in ('01B3','0101','0102','0103','0104')")
			// .append(" and t1.contract_flag = 'N' ")
			.append(" and t1.bank_type = 'BC' ")
	       	.append("  and t1.creditline is not null ")
	       	.append("  and t1.creditrange is not null ")
		    .append("  and t1.bank_account is not null ")
		    .append("  and t1.receive_bank  is not null ")
		    .append("  and t1.loan_term is not null ")
		    .append("  and t1.bank_osn is null ")
	       	.append(" and NOT EXISTS(SELECT 1 FROM o2o_datalist t3 where t3.loanapply_no=t1.loanapply_no")
	       	.append(" and t3.datalist_code='"+FileTypeConstants.DALIST_TYPE_REFUNDCONTRACTBAK+"' and t3.is_delete = 'N' )");
			if(StringUtil.hasText(startId)){
				sql.append("   AND t2.bank_applyno > '").append(startId).append("' ");
			};
			sql.append(" ORDER BY t2.bank_applyno LIMIT 1");
		return super.findEntity(sql.toString(), BOCS02Bean.class);
	}
	
	public BOCS02Element joinCrmInfo(BOCS02Bean bean){
		StringBuffer crmSql = new StringBuffer();
		crmSql.append(" select t1.customer_name 'RECEIVEPER',t1.customer_name 'LAFPPER',")	//放款用户名
			.append("t1.mobile_number 'PERMBLNO',")	//移动电话（住址电话）
			//.append("t2.recipient_address 'PERHMAD', ")//地址（住宅地址）
			//.append("t3.shopaddress 'PERHMAD', ")//门店地址
			.append("t3.shopprovince shopprovince, t3.shopcity shopcity, t3.shopcounty shopcounty,")
			.append("t3.licence_name 'PERCMPNM', ") 
			.append("t3.licence_address 'PERHMAD' ")			
			.append(" from customer_basic_info t1, customer_shop_info t3 ")
			.append(" where t1.customer_id = t3.customer_id ")
			.append(" and t1.customer_id = ?");
		BOCS02Element element = new BOCS02Element();
		try {
			BeanUtils.copyProperties(element, bean);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		BOCS02Bean customerBean =  crmJdbcSession.findEntity(crmSql.toString(), BOCS02Bean.class, bean.getCustomerId());
		if(null == customerBean){
			return null;
		}
		logger.info("-----customerBean PERCMPNM:"+customerBean.getPERCMPNM()+",PERHMAD:"+customerBean.getPERHMAD());
		element.setRECEIVEPER(customerBean.getRECEIVEPER());
		element.setPERMBLNO(customerBean.getPERMBLNO());
		element.setLAFPPER(customerBean.getLAFPPER());
		String unitNo = bean.getUnitNo();
		String unitSql = "select t.org_full_name from act_org t where t.unit_no = ?";
		String unitFullName = workflowJdbcSession.findString(unitSql, unitNo);
		element.setAGENCY(unitFullName);
		element.setPERHMAD(customerBean.getPERHMAD());
		element.setPERCMPNM(customerBean.getPERCMPNM());
		logger.info("------element PERCMPNM:"+element.getPERCMPNM()+",element:"+element.getPERHMAD());
		return element;
	}
	
	public String checkElement(BOCS02Element element){
		List<String> list = new LinkedList<>();
		if(StringUtil.isEmpty(element.getRECEIVEPER())){
			list.add("客户名称不能为空");
		}
		if(StringUtil.isEmpty(element.getLAFPACT())){
			list.add("银行放款/还款卡号不能为空");
		}
		if(StringUtil.isEmpty(element.getRECEIVEBNK())){
			list.add("贷款发放开户行不能为空");
		}
		if(StringUtil.isEmpty(element.getLAFTERM())){
			list.add("贷款期限不能为空");
		}
		if(StringUtil.isEmpty(element.getLAFDRAMT())){
			list.add("贷款金额不能为空");
		}
		if(StringUtil.isEmpty(element.getAGENCY())){
			list.add("经营单位不能为空");
		}
		if(StringUtil.isEmpty(element.getPERMBLNO())){
			list.add("移动电话不能为空");
		}
		if(StringUtil.isEmpty(element.getPERHMAD())){
			list.add("乙方地址");
		}
		if(StringUtil.isEmpty(element.getPERCMPNM())){
			list.add("乙方单位");
		}
		if(StringUtil.isEmpty(element.getAGENCY())){
			list.add("经营单位不能为空");
		}
		return StringUtils.join(list,",");
	}
	
	public int updateInfo(String appno, String bank_osn){
		String loanapplyNo = super.findString("select loanapply_no from o2o_loan_user where bank_applyno = ?", appno);
		String sql = "update o2o_loanapply oly set oly.bank_osn = ? where oly.loanapply_no = ?";
		return super.execute(sql, bank_osn, loanapplyNo);
	}
}
